package org.geometerplus.android.fbreader.util;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import org.geometerplus.zlibrary.ui.android.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: dou
 * Time: 18-11-30  上午9:54
 * Decription:
 */
public class PagerListView extends LinearLayout {

    Context mContext;

    List mData;

    int mPageIndex = 0;
    int mPageSize;
    int mPageCount;

    OnLoadListener mOnLoadListener;
    TextView tv_footer_info;
    private ListView listview;
    private View empty_view;
    private View foot_view;
    private ImageButton iv_load_next;
    private ImageButton iv_load_pre;

    public PagerListView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public PagerListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public PagerListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        initListView();
    }

    private void initListView() {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.layout_custom_list_page, null);
        addView(rootView);

        tv_footer_info = findViewById(R.id.tv_index);
        listview = findViewById(R.id.list_view);
        empty_view = findViewById(R.id.empty_view);
        foot_view = findViewById(R.id.foot_view);
        iv_load_next = rootView.findViewById(R.id.iv_load_next);
        iv_load_pre = rootView.findViewById(R.id.iv_load_pre);
        listview.setDivider(null);
        listview.setDividerHeight(0);

        iv_load_next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPageIndex >= mPageCount - 1)
                    return;

                mOnLoadListener.onLoad(++mPageIndex);
                updateFootInfo();
            }
        });

        iv_load_pre.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPageIndex <= 0)
                    return;
                mOnLoadListener.onLoad(--mPageIndex);
                updateFootInfo();
            }
        });
    }

    public void setupImageButtonState() {
        if (mPageIndex >= mPageCount - 1) {
            iv_load_next.setEnabled(false);
            iv_load_pre.setEnabled(true);
        } else if (mPageIndex <= 0) {
            iv_load_next.setEnabled(true);
            iv_load_pre.setEnabled(false);
        } else {
            iv_load_next.setEnabled(true);
            iv_load_pre.setEnabled(true);
        }
    }

    private void showEmptyView(int size) {
        foot_view.setVisibility(GONE);
        if (size > 0) {
            empty_view.setVisibility(GONE);
            listview.setVisibility(VISIBLE);
        } else {
            empty_view.setVisibility(VISIBLE);
            listview.setVisibility(GONE);
            LinearLayout parent = (LinearLayout) empty_view.getParent();
            parent.setGravity(Gravity.CENTER);
            ViewGroup.LayoutParams layoutParams = parent.getLayoutParams();
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            parent.setLayoutParams(layoutParams);
        }
    }

    public void setAdapter(BaseAdapter adapter) {
        if (listview != null)
            listview.setAdapter(adapter);
    }

    public void setPageSize(int size){
        mPageSize = size;
    }

    public void setData(List data, int size){
        mData = data;
        mPageSize = size;
        mPageCount = (int) Math.ceil((float)mData.size() / mPageSize);
        updateFootInfo();
        showEmptyView(CollectionUtils.getSize(mData));
    }

    public void updateFootInfo(){
        tv_footer_info.setText(String.format("%d/%d", (mPageIndex + 1), mPageCount));
        setupImageButtonState();
    }

    public List getPageData(int pageIndex){

        if (mData == null)
            return new ArrayList<>();

        ArrayList pageOutline = new ArrayList<>();

        if (pageIndex < mPageCount - 1) {
            for (int i = pageIndex * mPageSize; i < (pageIndex + 1) * mPageSize; i++){
                pageOutline.add(mData.get(i));
            }
        } else if (pageIndex == mPageCount - 1){
            for (int i = pageIndex * mPageSize; i < mData.size(); i++){
                pageOutline.add(mData.get(i));
            }
        }

        return pageOutline;
    }

    public void loadCompleted(){

    }

    public void setPageIndex(int index) {
        this.mPageIndex = index;
        updateFootInfo();
    }

    public void setOnLoadListener(OnLoadListener listener){
        this.mOnLoadListener = listener;
        mOnLoadListener.onLoad(mPageIndex);
    }

    public void load(){
        if (mOnLoadListener != null) {
            mOnLoadListener.onLoad(mPageIndex);
        }
    }

    public void reLoad(){
        if (mOnLoadListener != null) {
            List pageData = getPageData(mPageIndex);
            if (pageData != null && !pageData.isEmpty()) {
                mOnLoadListener.onLoad(mPageIndex);
            } else if (mPageIndex > 0){
                mPageIndex--;
                mOnLoadListener.onLoad(mPageIndex);
                updateFootInfo();
            } else {
                mOnLoadListener.onLoad(0);
            }
        }
    }

    public int getPageSize() {
        return mPageSize;
    }
}
